from dbs.mongo_db import MongoDB

user_table = MongoDB('user')
block_table = MongoDB('block')
task_table = MongoDB('task')


def get_user(filters):
    data = user_table.getAll(filter=filters, sort=[("_id", -1)])
    res = []
    for i in data:
        del i['_id']
        res.append(i)
    return res


def insert_user(user_data):
    user_table.insertOne(user_data)


def update_user(user_id, dict_data_updater):
    filter = {'user_id': user_id}
    updater = {"$set": dict_data_updater}
    user_table.updateOne(filter=filter, update=updater)
    return True


def get_block():
    data = block_table.getAll(sort=[("_id", 1)], )
    res = []
    for i in data:
        del i['_id']
        res.append(i)
    return res


def insert_block(block):
    print(block)
    block_table.insertOne(block)


def get_block_by_filters(filter_dict):
    data = block_table.getAll(filter=filter_dict, sort=[("_id", -1)], )
    res = []
    for i in data:
        del i['_id']
        res.append(i)
    return res


def insert_task(task_data):
    task_table.insertOne(task_data)


def get_task(filters):
    data = task_table.getAll(filter=filters, sort=[("_id", -1)])
    res = []
    for i in data:
        del i['_id']
        res.append(i)
    return res


def get_touzi_task(filters):
    with MongoDB('task_money_pool') as m:
        data = m.getAll(filter=filters)
        res = set()
        for i in data:
            res.add(i.get('task_id'))
    data = get_task({'task_id': {'$in': list(res)}})
    return data


def update_task(task_id, dict_data_updater):
    filter = {'task_id': task_id}
    updater = {"$set": dict_data_updater}
    task_table.updateOne(filter=filter, update=updater)
    return True


def update_task_money_pool(dict_data_updater):
    with MongoDB('task_money_pool') as m:
        m.insertOne(dict_data_updater)


def get_task_count():
    count = sum(1 for _ in task_table.getAll())
    return count


def get_task_comment(filters):
    with MongoDB('task_comment') as m:
        data = m.getAll(filter=filters, sort=[("_id", -1)])
        res = []
        for i in data:
            del i['_id']
            res.append(i)
        return res


def add_task_comment(task_comment):
    with MongoDB('task_comment') as m:
        m.insertOne(task_comment)


def add_task_tousu(task_comment):
    with MongoDB('task_tousu') as m:
        m.insertOne(task_comment)


def get_task_tousu(filters):
    with MongoDB('task_tousu') as m:
        data = m.getAll(filter=filters, sort=[("_id", -1)])
        res = []
        for i in data:
            del i['_id']
            res.append(i)
        return res


def get_touzi_list():
    with MongoDB('task_money_pool') as m:
        data = m.getAll(sort=[("_id", -1)])
        res = []
        for i in data:
            del i['_id']
            res.append(i)
        return res


def get_touzi_list_by_task(task_id):
    with MongoDB('task_money_pool') as m:
        data = m.getAll(filter={'task_id': task_id})
        res = []
        for i in data:
            del i['_id']
            res.append(i)
        return res


def get_block_by_index():
    data = block_table.getAll(sort=[("index", -1)], )
    res = []
    for i in data:
        del i['_id']
        res.append(i)
    return res


def update_tousu(tousu_id, dict_data_updater):
    with MongoDB('task_tousu') as m:
        filter = {'tousu_id': tousu_id}
        updater = {"$set": dict_data_updater}
        m.updateOne(filter=filter, update=updater)
        return True


def write_news(news):
    with open('api/datas/news.txt', 'a+', encoding='utf8') as f:
        f.write(news + '\n')


if __name__ == '__main__':
    a = get_task_count()
    print(a)
